﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class ConditionRule
    {
        private Type left;

        public Type Left
        {
            get { return left; }
        }
        private Type right;

        public Type Right
        {
            get { return right; }
        }
        private ConditionType[] supportedConditions;

        public ConditionType[] SupportedConditions
        {
            get { return supportedConditions; }
            //set { m_supportedConditions = value; }
        }

        public ConditionRule(Type left, Type right, ConditionType[] condTypeArr)
        {
            this.left = left;
            this.right = right;
            this.supportedConditions = condTypeArr;

        }
    }

    public static class ConditionRules
    {
        private static List<ConditionRule> rules;

        public static List<ConditionRule> Rules
        {
            get { return rules; }
        }

        static  ConditionRules()
        {
            if (rules == null)
            {
                rules = new List<ConditionRule>();

                //creation of basic rules
                rules.Add(new ConditionRule(typeof(bool), typeof(bool),
                  new ConditionType[] {   ConditionType.Equal, 
                                        ConditionType.NonEqual }));
                rules.Add(new ConditionRule(typeof(string), typeof(string),
                    new ConditionType[] {   ConditionType.Equal, 
                                        ConditionType.NonEqual }));
                rules.Add(new ConditionRule(typeof(int), typeof(int),
                    new ConditionType[] {   ConditionType.Equal, 
                                        ConditionType.NonEqual, 
                                        ConditionType.More, 
                                        ConditionType.Less, 
                                        ConditionType.MoreEqual,    
                                        ConditionType.LessEqual }));
            }
        }

        

        public static IEnumerable<ConditionRule> FindInRules(Type rightType, Type leftType, ConditionType condType)
        {
            List<ConditionRule> list = rules;
            IEnumerable<ConditionRule> ret;
            if (list == null)
            {
                throw new NullReferenceException();
            }
            ret = from r in list
                  where (r.Left.Equals(leftType) &&
                          r.Right.Equals(rightType) &&
                          r.SupportedConditions.Contains<ConditionType>(condType))
                  select r;
            return ret;
        }

    }
}
